<?php
class model_picture extends model
{
	function __construct()
	{
		parent::__construct();
		$this->_table = $this->db->options['prefix'].'picture';
		$this->_primary = 'pictureid';
		$this->_fields = array('pictureid','missionid','contentid','path','url','description');
		
		$this->_readonly = array('pictureid');
	}

	function add($data)
	{
		return $this->insert($data);
	}
	
	function get_by_contentid($contentid)
	{
		return $this->select("contentid=$contentid", "*", "pictureid ASC");
	}
	
	function delete_by_contentid($contentid)
	{
		return $this->delete_by("contentid", $contentid);
	}
	
	function delete_picture($contentid)
	{
		$data = $this->select("contentid=$contentid", "*", "pictureid DESC");
		foreach ($data as $v)
		{
			if(!file_exists(UPLOAD_PATH.$v['path']))
			{
				continue;
			}
			if(!@unlink(UPLOAD_PATH.$v['path']))
			{
				$this->error = '图片删除失败！';
				return false;
			};
		}
		return true;
	}
	
	function _after_select(& $data, $multiple = false)
	{
		if ($multiple)
		{
			array_map(array($this, 'output'), & $data);
		}
		else 
		{
			$this->output($data);
		}
	}
	
	function output(& $r)
	{
		$r['description'] = $r['description']?$r['description']:table('content',$r['contentid'],'title');
	}
}